package com.ikas.ai.framework.statistics.function;

import com.google.common.collect.Lists;
import com.ikas.ai.framework.statistics.StatisticType;

import java.util.Collections;
import java.util.List;

/**
 * @Description: 平均数
 * @Author: qi.yilai
 * @Date: 2023/10/27 18:10
 * @Version: 1.0.0
 **/
public class MedianCalcFunction implements CalcFunction<List<? extends Number>, Object, Object, Double> {

    @Override
    public Double calculate(List<? extends Number> list) {
        Collections.sort(list, (t1, t2) -> {
            if (t1.doubleValue() > t2.doubleValue()) {
                return 1;
            } else {
                return -1;
            }
        });
        int size =  list.size();
        if (size % 2 == 0) {
            return list.get(size /2).doubleValue();
        } else {
            return (list.get(size / 2).doubleValue() + list.get(size / 2 + 1).doubleValue()) / 2;
        }
    }

    @Override
    public Double calculate(List<? extends Number> list, Object o1, Object o2) {
        throw new UnsupportedOperationException("不支持利用临时变量简化计算");
    }

    @Override
    public StatisticType type() {
        return StatisticType.MEDIAN;
    }

    public static void main(String[] args) {
        System.out.println(new MedianCalcFunction().calculate(Lists.newArrayList(1, 56f, 43.45, -34.78d)));
    }
}
